Modular Polynomial Arithmetic

Module 03 / Lesson 05

Mathematical Visualization


Polynomials and Finite Fields

Modular polynomial arithmetic is a method where coefficients and polynomial results are reduced within a Finite Field (like $GF(2)$). It is the core logic used in AES (Advanced Encryption Standard) to ensure data blocks remain fixed in size.

Key Components:

  • Irreducible Polynomial: Acts like a prime number for reduction.
  • Modulo Reduction: Finding the remainder after division.
  • GF(2) Coefficients: Coefficients are only 0 or 1.

The Logic:

  • Multiplication: Perform standard algebraic multiplication.
  • Reduction: Divide the result by an irreducible polynomial.
  • Result: The remainder is our final encrypted/transformed state.

Example in GF(2³):

Irreducible: $x^3 + x + 1$
$(x^2) \times (x^2) = x^4$
$x^4 \pmod{x^3 + x + 1} = \mathbf{x^2 + x}$


Python Implementation (GF2 Poly Reduction)

def poly_to_int(poly_str):
    return int(poly_str, 2)

def gf2_poly_mod(a, m):
    # 'a' is the polynomial to reduce, 'm' is irreducible
    # Both are integers representing binary coefficients
    a_len = a.bit_length()
    m_len = m.bit_length()
    
    while a_len >= m_len:
        # Align 'm' with the highest bit of 'a'
        shift = a_len - m_len
        a ^= (m << shift) # XOR reduction
        a_len = a.bit_length()
    return a

# Example: x^4 (10000) mod x^3 + x + 1 (1011)
result = gf2_poly_mod(0b10000, 0b1011)
print(f"Remainder (Binary): {bin(result)}") # 0b11 -> x + 1